//
//  SYS_PublicPopView.m
//  System_App
//
//  Created by 魏配军 on 2017/11/29.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_PublicPopView.h"

@interface SYS_PublicPopView()

@end

@implementation SYS_PublicPopView
@synthesize title,message,cancelbtn,surebtn;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

-(void)initwithtitle:(NSString *)str andmessage:(NSString *)str1 andcancelbtn:(NSString *)cancel andotherbtn:(NSString *)other withIconStr:(NSString *) iconNameStr  andCancelBtnImageNameStr:(NSString *) cancelImageStr  andSureBtnImageNameStr:(NSString *)sureImageStr;
{
//    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    
    self.frame=CGRectMake(50, 150, SCREENWIDTH-100, 180);
    
    self.image=[UIImage imageNamed:@"SYS_Public_popBg1"];
    self.userInteractionEnabled = YES;
    self.layer.borderColor = [UIColor clearColor].CGColor;
    self.layer.borderWidth =  1;
    self.layer.cornerRadius = 10;
    [self.layer setMasksToBounds:YES];
    
    UIImageView *iconImageView=[UIImageView new];
    iconImageView.image=[UIImage imageNamed:iconNameStr];
    
    title = [[UILabel alloc] init];
    title.text = str;
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:16];
    title.textColor = DGHColor(0, 0, 0);
    
    message = [[UILabel alloc] init];
    message.textAlignment = NSTextAlignmentCenter;
    message.font = [UIFont systemFontOfSize:14];
    message.textColor = DGHColor(80, 80, 80);
    message.text = str1;
    
    [self sd_addSubviews:@[iconImageView,title,message]];
    
    iconImageView.sd_layout
    .topSpaceToView(self, 15)
    .leftSpaceToView(self, (self.width-25)/2)
    .widthIs(25)
    .heightIs(25);
    
    if (str1.length>0) {
        title.sd_layout
        .leftSpaceToView(self, 10)
        .topSpaceToView(iconImageView, 15)
        .widthIs(self.width-20)
        .heightIs(20);
        
        message.sd_layout
        .leftSpaceToView(self, 10)
        .topSpaceToView(title, 10)
        .widthIs(self.width-20)
        .autoHeightRatio(0);
        [message setMaxNumberOfLinesToShow:2];
        
    }else{
        title.font = [UIFont systemFontOfSize:18];

        title.sd_layout
        .leftSpaceToView(self, 10)
        .topSpaceToView(iconImageView, 25)
        .widthIs(self.width-20)
        .heightIs(20);
    }
    
    if (cancel == nil &&other != nil) {
        surebtn = [[SYS_PublicButton alloc]initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height-50, self.frame.size.width/2, 50)];
        [surebtn addTarget:self action:@selector(selectbtn:) forControlEvents:UIControlEventTouchUpInside];
        surebtn.iconImageView1.image=[UIImage imageNamed:sureImageStr];
        surebtn.nameLabel.text=other;
        surebtn.tag = 1001;
        [cancelbtn setBackgroundColor:DGHColor(253, 253, 253)];
        [surebtn setBackgroundColor:DGHColor(253, 253, 253)];
        
        title.textColor=DSCOLOR_WHITE;
        UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, self.height-50, self.width, 0.5)];
        lineView.backgroundColor=DGHColor(221, 221, 221);
        [self addSubview:lineView];
        [surebtn setBackgroundColor:DGHColor(255, 255, 255)];
        surebtn.frame = CGRectMake(0, self.frame.size.height-50, self.width, 50);
        surebtn.layer.borderColor = [UIColor clearColor].CGColor;
        surebtn.layer.borderWidth =  1;
        surebtn.layer.cornerRadius = 5;
        [surebtn.layer setMasksToBounds:YES];
        [self addSubview:surebtn];
    }else if(cancel != nil &&other == nil)
    {
        cancelbtn.frame = CGRectMake(10, self.frame.size.height-50, 260, 50);
        cancelbtn.layer.borderColor = [UIColor clearColor].CGColor;
        cancelbtn.layer.borderWidth =  1;
        cancelbtn.layer.cornerRadius = 5;
        [cancelbtn.layer setMasksToBounds:YES];
        [self addSubview:cancelbtn];
    }else if(cancel == nil &&other == nil)
    {
        
    }else
    {
        cancelbtn = [[SYS_PublicButton alloc]initWithFrame: CGRectMake(0, self.frame.size.height-50, self.frame.size.width/2, 50)];
        [cancelbtn addTarget:self action:@selector(selectbtn:) forControlEvents:UIControlEventTouchUpInside];
        cancelbtn.nameLabel.text=cancel;
        cancelbtn.iconImageView1.image=[UIImage imageNamed:cancelImageStr];
        cancelbtn.tag = 1000;
        
        surebtn = [[SYS_PublicButton alloc]initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height-50, self.frame.size.width/2, 50)];
        [surebtn addTarget:self action:@selector(selectbtn:) forControlEvents:UIControlEventTouchUpInside];
        surebtn.iconImageView1.image=[UIImage imageNamed:sureImageStr];
        surebtn.nameLabel.text=other;
        surebtn.tag = 1001;
        [cancelbtn setBackgroundColor:DGHColor(253, 253, 253)];
        [surebtn setBackgroundColor:DGHColor(253, 253, 253)];
        
        [self addSubview:cancelbtn];
        [self addSubview:surebtn];
        
        UIView *lineView1=[[UIView alloc]initWithFrame:CGRectMake(0,self.frame.size.height-50, self.width, 0.6)];
        lineView1.backgroundColor=DGHColor(221, 221, 221);
        [self addSubview:lineView1];
        
        UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width/2,self.frame.size.height-45, 0.6, 50)];
        lineView.backgroundColor=DGHColor(221, 221, 221);
        [self addSubview:lineView];
    }
}

-(id)initwithtitleText:(NSString *)titleText withMessage:(NSString *)messageText andcancelbtn:(NSString *)cancel andotherbtn:(NSString *)other withIconStr:(NSString *) iconNameStr andCancelBtnImageNameStr:(NSString *) cancelImageStr  andSureBtnImageNameStr:(NSString *)sureImageStr
{
    if (self==[super init]) {
        if (cancel==nil&&other==nil) {
            
            if (messageText.length==0) {
                self.frame=CGRectMake(40, 150, SCREENWIDTH-80, 110);
            }else{
                self.frame=CGRectMake(40, 150, SCREENWIDTH-80, 120);
            }

        }else{
            self.frame=CGRectMake(40, 150, SCREENWIDTH-80, 180);
  
        }
        
        self.image=[UIImage imageNamed:@"SYS_Public_popBg1"];
        self.userInteractionEnabled = YES;
        self.layer.borderColor = [UIColor clearColor].CGColor;
        self.layer.borderWidth =  1;
        self.layer.cornerRadius = 10;
        [self.layer setMasksToBounds:YES];
        
        UIImageView *iconImageView=[UIImageView new];
        iconImageView.image=[UIImage imageNamed:iconNameStr];
        
        title = [[UILabel alloc] init];
        title.text = titleText;
        title.textAlignment = NSTextAlignmentCenter;
        title.font = [UIFont systemFontOfSize:16];
        title.textColor = DGHColor(0, 0, 0);
        
        message = [[UILabel alloc] init];
        message.textAlignment = NSTextAlignmentCenter;
        message.font = [UIFont systemFontOfSize:14];
        message.textColor = DGHColor(80, 80, 80);
        message.text = messageText;
        
        [self sd_addSubviews:@[iconImageView,title,message]];
        
        iconImageView.sd_layout
        .topSpaceToView(self, 15)
        .leftSpaceToView(self, (self.width-25)/2)
        .widthIs(25)
        .heightIs(25);
        
        if (messageText.length>0) {
            title.sd_layout
            .leftSpaceToView(self, 10)
            .topSpaceToView(iconImageView, 15)
            .widthIs(self.width-20)
            .heightIs(20);
            
            message.sd_layout
            .leftSpaceToView(self, 10)
            .topSpaceToView(title, 10)
            .widthIs(self.width-20)
            .autoHeightRatio(0);
            [message setMaxNumberOfLinesToShow:2];
            
        }else{
            title.font = [UIFont systemFontOfSize:18];
            
            title.sd_layout
            .leftSpaceToView(self, 10)
            .topSpaceToView(iconImageView, 25)
            .widthIs(self.width-20)
            .autoHeightRatio(0);
            [title setMaxNumberOfLinesToShow:2];
        }
        
        if (cancel == nil &&other != nil) {
            surebtn = [[SYS_PublicButton alloc]initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height-50, self.frame.size.width/2, 50)];
            [surebtn addTarget:self action:@selector(selectbtn:) forControlEvents:UIControlEventTouchUpInside];
            surebtn.iconImageView1.image=[UIImage imageNamed:sureImageStr];
            surebtn.nameLabel.text=other;
            surebtn.tag = 1001;
            [cancelbtn setBackgroundColor:DGHColor(253, 253, 253)];
            [surebtn setBackgroundColor:DGHColor(253, 253, 253)];
            
            title.textColor=DSCOLOR_WHITE;
            UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, self.height-50, self.width, 0.5)];
            lineView.backgroundColor=DGHColor(221, 221, 221);
            [self addSubview:lineView];
            [surebtn setBackgroundColor:DGHColor(255, 255, 255)];
            surebtn.frame = CGRectMake(0, self.frame.size.height-50, self.width, 50);
            surebtn.layer.borderColor = [UIColor clearColor].CGColor;
            surebtn.layer.borderWidth =  1;
            surebtn.layer.cornerRadius = 5;
            [surebtn.layer setMasksToBounds:YES];
            [self addSubview:surebtn];
        }else if(cancel != nil &&other == nil)
        {
            cancelbtn.frame = CGRectMake(10, self.frame.size.height-50, 260, 50);
            cancelbtn.layer.borderColor = [UIColor clearColor].CGColor;
            cancelbtn.layer.borderWidth =  1;
            cancelbtn.layer.cornerRadius = 5;
            [cancelbtn.layer setMasksToBounds:YES];
            [self addSubview:cancelbtn];
        }else if(cancel == nil &&other == nil)
        {
            
        }else
        {
            cancelbtn = [[SYS_PublicButton alloc]initWithFrame: CGRectMake(0, self.frame.size.height-50, self.frame.size.width/2, 50)];
            [cancelbtn addTarget:self action:@selector(selectbtn:) forControlEvents:UIControlEventTouchUpInside];
            cancelbtn.nameLabel.text=cancel;
            cancelbtn.iconImageView1.image=[UIImage imageNamed:cancelImageStr];
            cancelbtn.tag = 1000;
            
            surebtn = [[SYS_PublicButton alloc]initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height-50, self.frame.size.width/2, 50)];
            [surebtn addTarget:self action:@selector(selectbtn:) forControlEvents:UIControlEventTouchUpInside];
            surebtn.iconImageView1.image=[UIImage imageNamed:sureImageStr];
            surebtn.nameLabel.text=other;
            surebtn.tag = 1001;
            [cancelbtn setBackgroundColor:DGHColor(253, 253, 253)];
            [surebtn setBackgroundColor:DGHColor(253, 253, 253)];
            
            [self addSubview:cancelbtn];
            [self addSubview:surebtn];
            
            UIView *lineView1=[[UIView alloc]initWithFrame:CGRectMake(0,self.frame.size.height-50, self.width, 0.6)];
            lineView1.backgroundColor=DGHColor(221, 221, 221);
            [self addSubview:lineView1];
            
            UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width/2,self.frame.size.height-45, 0.6, 50)];
            lineView.backgroundColor=DGHColor(221, 221, 221);
            [self addSubview:lineView];
        }
    }
    return self;
}

//点击
-(void)selectbtn:(id)sender
{
    [self dismmis1];
    UIButton *btn = (UIButton *)sender;
    [self.delegate btnindex:(int)btn.tag];
}
//出现
-(void)show
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    _view = [[UIView alloc] initWithFrame:keyWindow.bounds];
    _view.alpha = 0.3;
    _view.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc]init];
    [gesture addTarget:self action:@selector(dismmis1)];
    [_view addGestureRecognizer:gesture];
    [keyWindow addSubview:_view];
    
    self.alpha = 1;
    [keyWindow addSubview:self];
    [keyWindow bringSubviewToFront:self];
    
    
}

//消失
-(void)dismmis1
{
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^
     {
         self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeScale(0.01, 0.01));
         _view.alpha = 0;
     }
                     completion:^(BOOL finished)
     {
         [_view removeFromSuperview];
         [self removeFromSuperview];
         
     }];
}



@end
